<template>
<div>
  <!--搜素表单-->
  <div style="margin-bottom: 20px">
    <el-input style="width: 240px" placeholder="请输入分类名称" v-model="params.name"></el-input>
    <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
    <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
  </div>
  <el-table :data="tableData" stripe row-key="id" default-expand-all :tree-props="{children: 'children'}">
    <el-table-column prop="id" label="编号" width="80"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
    <el-table-column prop="remark" label="备注"></el-table-column>
    <el-table-column prop="createtime" label="创建时间"></el-table-column>
    <el-table-column prop="updatetime" label="更新时间"></el-table-column>
  </el-table>
  <div style="margin-top: 20px">
    <el-pagination
        background
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="total">
    </el-pagination>
  </div>
  <!-- 添加子分类的隐藏窗口 -->
  <el-dialog title="添加子分类" :visible.sync="dialogFormVisible" width="30%">
    <el-form :model="form" label-width="100px" ref="ruleForm" :rules="rules">
      <el-form-item label="分类名称" prop="name" >
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark" >
        <el-input v-model="form.remark" autocomplete="off" ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";
export default {
  name: 'CategoryList',
  data(){
    //初始化数据
    return{
      user: Cookies.get('user')?JSON.parse(Cookies.get('user')):{},
      tableData: [],
      total: 0,
      dialogFormVisible:false,
      form:{},
      pid: null,
      params: {
        name: '',
        pageNum:1,
        pageSize:10,
      },
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.load();
  },
  methods:{
    load(){
      request.get('/category/page',{
        params: this.params
      }).then(res=> {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset(){
      this.params={
        pageNum: 1,
        pageSize: 10,
        name:'',
      }
      this.load()
    },
    handleCurrentChange(pageNum) {
      //点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.load()
    },
    handleAdd(row){
      //当前行id作为二级分类的pid
      this.pid=row.id
      this.dialogFormVisible=true
    },
  }
}
</script>
